import { request } from '../../request/index.js';
Page({

  data: {
    id: 11,
    userId: "0",
    title: "kmgzs",
    price: 100, 
    description: '狂码工作室欢迎你',
    upTime: '2021-11-11',
    imgs: [],
    disclaimer: [//免责声明
      { number: "1.", content: "本平台暂不支持线上交易。" },
      { number: "2.", content: "本平台不提供任何虚拟物品交易。" },
      { number: "3.", content: "所展示的商品供求信息由买卖双方自行提供，其真实性、准确性和合法性由发布人负责。" },
      { number: "4.", content: "请勿线上与卖家直接进行交易，防止被骗。" },
      { number: "5.", content: "线下交易地址请选择白天公共场所进行。" },
      { number: "注:", content: "无客服参与卖家与买家之间的谈话，交易地点、时间和交易额度都由双方互相协商而定，无任何手续费用和押金。" }
    ],
    //轮播图
    current: 0,// 页面swiper的current索引
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = JSON.parse(options.nextData)
    this.setData({
      id: data.id,
      userId: data.userId,
      title: data.title,
      price: data.price,
      description: data.description,
      upTime: data.upTime,
      imgs: data.imgs,
    })
  },
  swiperChange: function (e) {
    var that = this;
    if (e.detail.source == 'touch') {
      that.setData({
        current: e.detail.current
      })
    }
  },

  async toCollection() {
    let fav = { "cid": 0, "userId": 0, "cTime": "" }
    fav.cid = this.data.id
    fav.userId = wx.getStorageSync("userId")
    let myDate = new Date()
    fav.cTime = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate() + " " + myDate.getHours() + ":" + myDate.getMinutes()+":"+myDate.getSeconds()

    const { data } = await request({
      url: `/commodityFavorites/add/${JSON.stringify(fav)}`
    })
    if(data.state===1){
      wx.showToast({
        title: '收藏成功',
        mask: true,
      })
      
    }else{
      wx.showToast({
        title: '你已收藏',
        icon: 'none',
        mask: true,
      })
    }
  },

  preview(event) {
    let index = event.currentTarget.dataset.index
    console.log(this.data.imgs[index])
    wx.previewImage({
      current: this.data.imgs[index], // 当前显示图片的http链接
      urls: this.data.imgs // 需要预览的图片http链接列表
    })
  },
})